package com.starsea.book.ui.newpage

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityCategoryDetailsBinding
import com.starsea.book.databinding.LayoutCategorySortHeaderBinding
import com.starsea.book.ui.book.info.BookInfoActivity
import com.starsea.book.ui.newpage.adapter.CategorySortSortAdapter
import com.starsea.book.ui.newpage.adapter.HomeAdapter
import com.starsea.book.ui.newpage.entity.FlowContentEntity
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-01-11 20 GMT+8
 * @desc :
 */
class CategoryDetailsActivity : VMBaseActivity<ActivityCategoryDetailsBinding, HomeViewModel>() {

    override val binding by viewBinding(ActivityCategoryDetailsBinding::inflate)
    override val viewModel by viewModels<HomeViewModel>()

    lateinit var mAdapter: HomeAdapter

    var typeId: Int = 0

    var typeTitle: String? = ""

    var bookState: String = "-1"
    var labelTitle: String = ""
    var pageNo: Int = 1
    var sortType: String = "-1"
    var wordCount: String = "-1"

    var headerBinding: LayoutCategorySortHeaderBinding? = null

    lateinit var adapterLable: CategorySortSortAdapter
    lateinit var adapterState: CategorySortSortAdapter
    lateinit var adapterWordCount: CategorySortSortAdapter
    lateinit var adapterSort: CategorySortSortAdapter


    override fun onActivityCreated(savedInstanceState: Bundle?) {

        typeTitle = intent.getStringExtra("typeTitle")

        typeId = intent.getIntExtra("typeId", 0)

        title = typeTitle ?: ""

        binding.titleBar.setTitle(typeTitle ?: "")

        mAdapter = HomeAdapter(this) {
            startActivity<BookInfoActivity> {
                putExtra("bookId", it.bookId)
                putExtra("name", it.bookTitle)
                putExtra("author", it.authorName)
            }
        }

        binding.rvContent.layoutManager = LinearLayoutManager(this)

        binding.rvContent.adapter = mAdapter

        headerBinding =
            LayoutCategorySortHeaderBinding.inflate(layoutInflater, binding.rvContent, false)

        mAdapter.addHeaderView {
            headerBinding!!
        }

        adapterLable = CategorySortSortAdapter() {
            labelTitle = it.value
            pageNo = 1
            getListData()
        }
        adapterState = CategorySortSortAdapter() {
            bookState = it.value
            pageNo = 1
            getListData()
        }
        adapterWordCount = CategorySortSortAdapter() {
            wordCount = it.value
            pageNo = 1
            getListData()
        }
        adapterSort = CategorySortSortAdapter() {
            sortType = it.value
            pageNo = 1
            getListData()
        }

        headerBinding!!.flLabel.setAdapter(adapterLable)
        headerBinding!!.flState.setAdapter(adapterState)
        headerBinding!!.flWordcount.setAdapter(adapterWordCount)
        headerBinding!!.flSort.setAdapter(adapterSort)


        binding.srlRefresh.setOnRefreshListener {

            pageNo = 1
            getListData()

        }

        binding.srlRefresh.setOnLoadMoreListener {

            pageNo++
            getListData()

        }

        getListData()
        viewModel.categoryLable(
            typeId = typeId
        )


        viewModel.dataCategoryLableResult.observe(this) {
            if (it.status == 200) {

                val lableList: MutableList<FlowContentEntity> = arrayListOf()

                LogUtils.e(">>>> ", "" + it.data!!.labelTitleList?.toString())

                it.data!!.labelTitleList?.forEachIndexed { index, it ->
                    val entity = FlowContentEntity()
                    entity.isSelect = index == 0
                    entity.content = it.name ?: ""
                    entity.value = it.value ?: ""
                    lableList.add(entity)
                }

                adapterLable.addData(lableList)

                val stateList: MutableList<FlowContentEntity> = arrayListOf()

                it.data!!.bookStateList?.forEachIndexed { index, it ->
                    val entity = FlowContentEntity()
                    entity.isSelect = index == 0
                    entity.content = it.name ?: ""
                    entity.value = it.value ?: ""
                    stateList.add(entity)
                }
                adapterState.addData(stateList)

                val wordCountList: MutableList<FlowContentEntity> = arrayListOf()

                it.data!!.wordCountList?.forEachIndexed { index, it ->
                    val entity = FlowContentEntity()
                    entity.isSelect = index == 0
                    entity.content = it.name ?: ""
                    entity.value = it.value ?: ""
                    wordCountList.add(entity)
                }
                adapterWordCount.addData(wordCountList)

                val sortList: MutableList<FlowContentEntity> = arrayListOf()

                it.data!!.sortTypeList?.forEachIndexed { index, it ->
                    val entity = FlowContentEntity()
                    entity.isSelect = index == 0
                    entity.content = it.name ?: ""
                    entity.value = it.value ?: ""
                    sortList.add(entity)
                }

                adapterSort.addData(sortList)


            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@CategoryDetailsActivity)
            } else {
                toastOnUi(it.msg)
            }
        }


        viewModel.dataCategoryDetailsResult.observe(this) {
            if (pageNo == 1) {
                binding.srlRefresh.finishRefresh()
                mAdapter.setItems(it.data!!)
            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@CategoryDetailsActivity)
            } else {
                binding.srlRefresh.finishLoadMore()
                mAdapter.addItems(it.data!!)
            }

            if (it.data!!.size < 10) {
                binding.srlRefresh.finishLoadMoreWithNoMoreData()
            }
        }
    }

    private fun getListData() {
        viewModel.categoryDetails(
            bookState = bookState, labelTitle = labelTitle, pageNo = pageNo,
            sortType = sortType, wordCount = wordCount, typeId = typeId
        )
    }
}